﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utility
{
    public class GeoUtility
    {
        #region 距离计算
        public static double DistanceOfTwoPoints(double lon1, double lat1, double lon2, double lat2)
        {
            double radLat1 = Rad(lat1);
            double radLat2 = Rad(lat2);
            double a = radLat1 - radLat2;
            double b = Rad(lon1) - Rad(lon2);
            double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
             Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            s = s * 6378137.0;
            s = Math.Round(s * 10000) / 10000;

            //米转公里
            s = s / 1000;
            return s;
        }

        private static double Rad(double d)
        {
            return d * Math.PI / 180.0;
        }

        /// <summary>
        /// 判断一个经纬度是否有效
        /// </summary>
        /// <param name="lon"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        public static bool IsEffectiveLatLon(double lat, double lon)
        {
            if ((lon == 0d || lon == -1d) && (lat == 0d || lat == -1d))
            {//本系统把0 ,-1 当做无效坐标，在正常的情况 0 ,-1 是有效的
                return false;
            }

            if (Math.Abs(lon) > 180 || Math.Abs(lat) > 90)
            {
                return false;
            }
            return true;
        }
        #endregion
    }
}
